<?php
//订单控制器
class Controller_App_Order extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/order/detail";
        parent::__construct($req, $res);
    }

	//订单列表
    public function action_list ($type = NULL)
    {
        App_Order::autoClose($this->_user);
        $type   = empty($type) ? 'doing' : $type;

        $orders = App_Order::listByUser($this->_user, $type);
        $orders = App_Show_Config::getOrderShowMoney($orders);

        //print_r($orders);exit;
        $this->template->set('orders', $orders);
        $this->template->set('type', $type);
        $this->template->set_filename('smarty:app/order/list');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的订单', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('block', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

	// 用户红包订单列表
    public function action_hongbao ()
    {
        $orders = App_Order_Gift::listByUser($this->_user);
        $this->template->set('orders', $orders);
        $this->template->set_filename('smarty:app/order/hongbao');
    }

    // 订单支付结果
    public function action_payresult ($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $status = Arr::get($orderDetail, 'status');
        if ($status == 1)
        {
            // 团购订单，且本人就是开团人, 直接跳转到团购详情页
            if ($orderDetail['tuanId'] > 0 && $orderDetail['orderType'] == 'TUAN')
            {
                $tuanId     = $orderDetail['tuanId'];
                $tuanDetail = App_Order_Tuan::info($tuanId);
                $masterUid  = Arr::get($tuanDetail, 'createrUid');
                if ($masterUid == $this->_uid)
                {
                    $this->request->redirect('/tuan/detail/'. $tuanId);
                }
            }
            
            $this->template->set_filename('smarty:app/order/success');

            // 猜你喜欢商品列表
            $goodsList = App_Hot::items('GOODS', 0, 2);
            $this->template->set('goodsItems', $goodsList);

            // 订单详情
            $this->template->set('order', $orderDetail);
            $this->template->set('title', '支付成功');
            $this->initApp('支付成功', FALSE);
        }
        else
        {
            // 转向订单详情页
            $this->request->redirect('/order/detail/'.$orderNo);
        }
    }

    // 确认收货
    public function action_finish ($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $status = Arr::get($orderDetail, 'status');

        // 非下单用户，404
        if ($this->_uid != $orderDetail['uid'])
            throw new Http_Excepton_404();

        // 不是已发货状态，转向订单详情
        if ($status != 2)
        {
            $this->request->redirect('/order/detail/'.$orderNo);
            return;
        }

        App_Order::setOrderFinished($orderNo);
        $this->request->redirect('/order/detail/'.$orderNo);
    }

    // 订单评价
    public function action_evaluation ($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $status = Arr::get($orderDetail, 'status');

        if ($this->_uid != $orderDetail['uid'])
            throw new Http_Excepton_404();

        if ($status >= 1)
        {
            if (! empty($this->_post))
            {
                $goodsIds = Arr::get($this->_post, 'goodsId', array());
                $miaoshu  = Arr::get($this->_post, 'miaoshu', array());
                $fuwu     = Arr::get($this->_post, 'fuwu', array());
                $fahuo    = Arr::get($this->_post, 'fahuo', array());
                $wuliu    = Arr::get($this->_post, 'wuliu', array());
                $comments = Arr::get($this->_post, 'comments', array());

                $result   = array();

                foreach ($goodsIds as $index => $goodsId)
                {
                    $result[$goodsId] = array(
                        'miaoshu' => Arr::get($miaoshu, $index, 5),
                        'fuwu'    => Arr::get($fuwu, $index, 5),
                        'fahuo'   => Arr::get($fahuo, $index, 5),
                        'wuliu'   => Arr::get($wuliu, $index, 5),
                        'comments'=> Arr::get($comments, $index, ''),
                    );
                }

                $pinglun = new App_Order_Pinglun();
                $pinglun->pinglun($orderDetail, $result);
//                App_Order::pinglun($orderDetail, $result);
                $this->request->redirect('/order/detail/'.$orderNo);
            }
            else
            {
                $pinglun = new App_Order_Pinglun();
                $result  = $pinglun->detail($orderDetail['id']);
	

                if (empty($result))
                {
	                // 订单详情
	                $this->template->set('order', $orderDetail);
	                $this->template->set_filename('smarty:app/order/evaluation');
                }
                else
                {
                    $goodsList = array();
                    foreach ($orderDetail['arrGoodsInfo'] as $goodsItem)
                    {
                        $goods   = $goodsItem['goods'];
                        $goodsId = $goods['id'];
                        if (isset($result[$goodsId]))
                            $goods['pinglun'] = $result[$goodsId];

                        $goodsList[] = $goods;
                    }

	                $this->template->set('order', $orderDetail);
                    $this->template->set('goodsList', $goodsList);
	                $this->template->set_filename('smarty:app/order/evaluation_result');
                }
	        
	            $title_json  = App_Util::buildClientTitleJson('back', 'block', '订单评价', 'message', 'none', '');
	            $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
	            $this->template->set('title_json',   $title_json);
	            $this->template->set('bottom_json',  $bottom_json);
            }
        }
        else
        {
            $this->request->redirect('/order/detail/'.$orderNo);
        }
    }

    // 支付订单
    public function action_pay ($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $status = Arr::get($orderDetail, 'status');
        if ($status == 0)
        {
            $this->template->set('title', '订单支付');
            $this->template->set('order', $orderDetail);
            $this->template->set('payment', App_Payment::infoList ());
            $this->template->set_filename('smarty:app/order/payment');
            
            $title_json  = App_Util::buildClientTitleJson('back', 'block', '支付方式', 'message', 'none', '');
            $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');

            $this->template->set('title_json',   $title_json);
            $this->template->set('bottom_json',  $bottom_json);
        }
        else
        {
            $this->request->redirect('/order/detail/'.$orderNo);
        }
    }

	// 订单详细
    public function action_detail ($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);

        $arrGoodsInfo = Arr::get($orderDetail, 'arrGoodsInfo', array());
        
        if($orderDetail['uid'] != $this->_uid){
            $this->request->redirect('/');
        }
        
        $goodsList    = array();
        foreach ($arrGoodsInfo as $goodsItem)
        {
            $goods          = $goodsItem['goods'];
            $goods['money'] = array(
                'money'    => $goodsItem['money'],
                'quantity' => $goodsItem['quantity'],
                'price'    => $goodsItem['price'],
            );
            $goodsList[] = $goods;
        }

        // 按店铺分组的商品信息
        $arrBuyShops = App_Goods::goodsInfoGroupByShop ($goodsList);

        //print_r($orderDetail);exit;
        $this->template->set('goodsShopList', $arrBuyShops);

        $this->template->set('title', '订单详细');
        $this->template->set('order', $orderDetail);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '订单详情', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');

        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 待别人支付的订单详情
    public function action_waitForOtherPaid($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $this->template->set('title', '订单详细');
        $this->template->set('order', $orderDetail);
        $this->template->set_filename("smarty:app/order/detail_paidByOther");
    }

    // 送礼的订单详情
    public function action_gift($orderNo)
    {
        $orderDetail = App_Order::detail($orderNo);
        $orderGift   = App_Order_Gift::detail($orderNo, $this->_user);
        $userAddrList= App_Address::infoList($this->_user);
        $countsLeft  = $orderDetail['countsQuantity'] - $orderGift['countsGift'];
        $countsLeft  = $countsLeft >= 0 ? $countsLeft : 0;

        $this->template->set('title', '订单详细');
        $this->template->set('order', $orderDetail);
        $this->template->set('gift', $orderGift);
        $this->template->set('address', $userAddrList);
        $this->template->set('countsLeft', $countsLeft);
        $this->template->set_filename("smarty:app/order/detail_gift");

        $goodsImgUrl = App_Order_Gift::shareImageUrl($orderDetail);
        $this->initWechatShare('这是我送给你的礼物', $goodsImgUrl, '请点击领取或继续转送朋友');
    }

    // 我要礼物
    public function action_getGift()
    {
        $orderNo = Arr::get($this->_post, 'orderNo');
        $addrId  = Arr::get($this->_post, 'addressId');

        if (! empty($orderNo) && $addrId > 0)
            App_Order_Gift::addGift($orderNo, $this->_user, $addrId);

        $this->request->redirect('/m/order/gift/' . $orderNo);
    }

    // 取消订单
    public function action_remove ($orderNo)
    {
        $order = App_Order::basicInfo($orderNo);
        $bUid  = Arr::get($order, 'uid');
        $uid   = $this->_uid;

        if (empty($order) || $uid != $bUid)
            throw new Http_Exception_404();

        App_Order::remove(Arr::get($order, 'id'));
        $this->request->redirect('/order/list');
    }


    //
    // 创建订单
    //
    public function action_create ($orderType = 'normal')
    {
        $post = $this->request->post();

        $arrBuyInfo     = array();
        $tuanId         = Arr::get($post, 'tuanId', 0);
        $goodsIds       = Arr::get($post, 'goodsId');
        $goodsAttrIds   = Arr::get($post, 'goodsAttrId');
        $goodsPrices    = Arr::get($post, 'goodsPrice');
        $goodsQuantities= Arr::get($post, 'goodsQuantity');
        $index = 0;
        foreach ($goodsIds as $goodsId)
        {
            $arrBuyInfo[] = array(  
                'goodsId' => $goodsIds[$index],
                'goodsAttrId'   => $goodsAttrIds[$index],
                'goodsPrice'    => $goodsPrices[$index],
                'goodsQuantity' => $goodsQuantities[$index],
            );

            $index++; 
        }

        $coinMoney = Arr::get($post, 'coinMoney', 0);
        $ticketId  = Arr::get($post, 'ticketId', 0);
        
        $addressId = Arr::get($post, 'addressId', 0);
        $deliverId = Arr::get($post, 'deliverId', 0);

        $paymentId = Arr::get($post, 'paymentId', 0);

        $comments  = Arr::get($post, 'comments', '');

        $invoiceInfo = array(
            'invoiceType'    => Arr::get($post, 'invoiceType', 0),
            'invoiceContent' => Arr::get($post, 'invoiceContent', 0),
            'invoiceTitle'   => Arr::get($post, 'invoiceTitle', ''),
        );

        try {
            if ($orderType == 'paidByOther')
            // 代付订单
            {
                $orderNo = App_Order::create($arrBuyInfo, $coinMoney, $ticketId, $addressId, $deliverId, $paymentId, $invoiceInfo, $comments, $orderType);
                $this->request->redirect('/m/order/waitForOtherPaid/' . $orderNo);
            }
            else if ($orderType == 'toFriends')
            {
                $orderNo = App_Order::create($arrBuyInfo, $coinMoney, $ticketId, $addressId, $deliverId, $paymentId, $invoiceInfo, $comments, $orderType);
                $this->request->redirect('/m/order/gift/' . $orderNo);
            }
            // 拼单订单
            else if ($orderType == 'tuan')
            {
                $orderNo = App_Order::create($arrBuyInfo, $coinMoney, $ticketId, $addressId, $deliverId, $paymentId, $invoiceInfo, $comments, $orderType, $tuanId);
                $this->request->redirect('/order/pay/' . $orderNo);
            }
            else
            // 普通订单
            {
                $orderNo = App_Order::create($arrBuyInfo, $coinMoney, $ticketId, $addressId, $deliverId, $paymentId, $invoiceInfo, $comments, $orderType);
                $this->request->redirect('/order/pay/' . $orderNo);
            }
        } 
        catch (Exception $e)
        {
            //
            echo $e->getMessage();exit;
        }
    }
    
    public function before ()
    {
            parent::before();
            if ($this->_uid == 0)
            {
                    $this->request->redirect('/login');
            }
    }
}
